using System;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace haxeProject {

	public class PluginUI : System.Windows.Forms.UserControl {
		public System.Windows.Forms.RadioButton pJS;
		public System.Windows.Forms.TextBox pClass;
		private System.Windows.Forms.MenuItem break1;
		private System.Windows.Forms.Label LCommand;
		private System.Windows.Forms.Button Html;
		private System.Windows.Forms.MenuItem omAdd;
		public System.Windows.Forms.TextBox pExtra;
		private System.Windows.Forms.ContextMenu omenu;
		private System.Windows.Forms.MenuItem omDelete;
		private System.Windows.Forms.Label LLib;
		private System.Windows.Forms.MenuItem omSelect;
		public System.Windows.Forms.CheckBox pDebug;
		public System.Windows.Forms.GroupBox Props;
		public System.Windows.Forms.TextBox pOutput;
		public System.Windows.Forms.RadioButton pNeko;
		private System.Windows.Forms.Button LibBrowse;
		public System.Windows.Forms.TreeView Outputs;
		private System.Windows.Forms.Label LExtra;
		private System.Windows.Forms.Button ClassBrowse;
		public System.Windows.Forms.Label PName;
		private System.Windows.Forms.MenuItem omActive;
		public System.Windows.Forms.CheckBox pMain;
		public System.Windows.Forms.RadioButton pFlash9;
		public System.Windows.Forms.TextBox pLib;
		private System.Windows.Forms.MenuItem omRename;
		public System.Windows.Forms.RadioButton pPHP;
		private System.Windows.Forms.Label LClass;
		public System.Windows.Forms.RadioButton pFlash;
		private System.Windows.Forms.Label LPOut;
		public System.Windows.Forms.TextBox pCommand;
		private PluginMain plugin;

		public PluginUI(PluginMain p) {
			InitializeComponent();
			this.plugin = p;
			Outputs.ImageList = Icons.list;			
		}

		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.pCommand = new System.Windows.Forms.TextBox();
			this.LPOut = new System.Windows.Forms.Label();
			this.pFlash = new System.Windows.Forms.RadioButton();
			this.LClass = new System.Windows.Forms.Label();
			this.pPHP = new System.Windows.Forms.RadioButton();
			this.omRename = new System.Windows.Forms.MenuItem();
			this.pLib = new System.Windows.Forms.TextBox();
			this.pFlash9 = new System.Windows.Forms.RadioButton();
			this.pMain = new System.Windows.Forms.CheckBox();
			this.omActive = new System.Windows.Forms.MenuItem();
			this.PName = new System.Windows.Forms.Label();
			this.ClassBrowse = new System.Windows.Forms.Button();
			this.LExtra = new System.Windows.Forms.Label();
			this.Outputs = new System.Windows.Forms.TreeView();
			this.LibBrowse = new System.Windows.Forms.Button();
			this.pNeko = new System.Windows.Forms.RadioButton();
			this.pOutput = new System.Windows.Forms.TextBox();
			this.Props = new System.Windows.Forms.GroupBox();
			this.pDebug = new System.Windows.Forms.CheckBox();
			this.omSelect = new System.Windows.Forms.MenuItem();
			this.LLib = new System.Windows.Forms.Label();
			this.omDelete = new System.Windows.Forms.MenuItem();
			this.omenu = new System.Windows.Forms.ContextMenu();
			this.pExtra = new System.Windows.Forms.TextBox();
			this.omAdd = new System.Windows.Forms.MenuItem();
			this.Html = new System.Windows.Forms.Button();
			this.LCommand = new System.Windows.Forms.Label();
			this.break1 = new System.Windows.Forms.MenuItem();
			this.pClass = new System.Windows.Forms.TextBox();
			this.pJS = new System.Windows.Forms.RadioButton();
			this.Props.SuspendLayout();
			this.SuspendLayout();
			// 
			// pCommand
			// 
			this.pCommand.Location = new System.Drawing.Point(64, 176);
			this.pCommand.Name = "pCommand";
			this.pCommand.Size = new System.Drawing.Size(152, 20);
			this.pCommand.TabIndex = 20;
			this.pCommand.Text = "";
			this.pCommand.TextChanged += new System.EventHandler(this.propChanged);
			// 
			// LPOut
			// 
			this.LPOut.Location = new System.Drawing.Point(8, 20);
			this.LPOut.Name = "LPOut";
			this.LPOut.Size = new System.Drawing.Size(64, 24);
			this.LPOut.TabIndex = 0;
			this.LPOut.Text = "Output :";
			// 
			// pFlash
			// 
			this.pFlash.Location = new System.Drawing.Point(3, 48);
			this.pFlash.Name = "pFlash";
			this.pFlash.Size = new System.Drawing.Size(48, 16);
			this.pFlash.TabIndex = 7;
			this.pFlash.Text = "Flash";
			this.pFlash.CheckedChanged += new System.EventHandler(this.outputChanged);
			// 
			// LClass
			// 
			this.LClass.Location = new System.Drawing.Point(8, 100);
			this.LClass.Name = "LClass";
			this.LClass.Size = new System.Drawing.Size(48, 24);
			this.LClass.TabIndex = 11;
			this.LClass.Text = "Class :";
			// 
			// pPHP
			// 
			this.pPHP.Location = new System.Drawing.Point(180, 48);
			this.pPHP.Name = "pPHP";
			this.pPHP.Size = new System.Drawing.Size(42, 16);
			this.pPHP.TabIndex = 22;
			this.pPHP.Text = "PHP";
			this.pPHP.CheckedChanged += new System.EventHandler(this.outputChanged);
			// 
			// omRename
			// 
			this.omRename.Index = 3;
			this.omRename.Text = "Rename";
			this.omRename.Click += new System.EventHandler(this.menuCommand);
			// 
			// pLib
			// 
			this.pLib.Location = new System.Drawing.Point(64, 68);
			this.pLib.Name = "pLib";
			this.pLib.Size = new System.Drawing.Size(120, 20);
			this.pLib.TabIndex = 17;
			this.pLib.Text = "";
			this.pLib.TextChanged += new System.EventHandler(this.propChanged);
			// 
			// pFlash9
			// 
			this.pFlash9.Location = new System.Drawing.Point(48, 48);
			this.pFlash9.Name = "pFlash9";
			this.pFlash9.Size = new System.Drawing.Size(54, 16);
			this.pFlash9.TabIndex = 10;
			this.pFlash9.Text = "Flash9";
			this.pFlash9.CheckedChanged += new System.EventHandler(this.outputChanged);
			// 
			// pMain
			// 
			this.pMain.Location = new System.Drawing.Point(16, 128);
			this.pMain.Name = "pMain";
			this.pMain.Size = new System.Drawing.Size(152, 16);
			this.pMain.TabIndex = 14;
			this.pMain.Text = "Call main() function";
			this.pMain.CheckedChanged += new System.EventHandler(this.propChanged);
			// 
			// omActive
			// 
			this.omActive.Index = 5;
			this.omActive.Text = "Active";
			this.omActive.Click += new System.EventHandler(this.menuCommand);
			// 
			// PName
			// 
			this.PName.Location = new System.Drawing.Point(8, 8);
			this.PName.Name = "PName";
			this.PName.Size = new System.Drawing.Size(216, 16);
			this.PName.TabIndex = 1;
			this.PName.Text = "<Project File>";
			// 
			// ClassBrowse
			// 
			this.ClassBrowse.Location = new System.Drawing.Point(192, 96);
			this.ClassBrowse.Name = "ClassBrowse";
			this.ClassBrowse.Size = new System.Drawing.Size(24, 20);
			this.ClassBrowse.TabIndex = 13;
			this.ClassBrowse.Text = "...";
			this.ClassBrowse.Click += new System.EventHandler(this.ClassBrowseClick);
			// 
			// LExtra
			// 
			this.LExtra.Location = new System.Drawing.Point(8, 216);
			this.LExtra.Name = "LExtra";
			this.LExtra.Size = new System.Drawing.Size(104, 16);
			this.LExtra.TabIndex = 6;
			this.LExtra.Text = "Extra Parameters :";
			// 
			// Outputs
			// 
			this.Outputs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.Outputs.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Outputs.FullRowSelect = true;
			this.Outputs.ImageIndex = -1;
			this.Outputs.LabelEdit = true;
			this.Outputs.Location = new System.Drawing.Point(8, 32);
			this.Outputs.Name = "Outputs";
			this.Outputs.Scrollable = false;
			this.Outputs.SelectedImageIndex = -1;
			this.Outputs.Size = new System.Drawing.Size(208, 128);
			this.Outputs.TabIndex = 0;
			this.Outputs.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OutputsMouseUp);
			this.Outputs.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OutputsAfterSelect);
			this.Outputs.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.OutputsAfterLabelEdit);
			this.Outputs.BeforeLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.OutputsBeforeLabelEdit);
			// 
			// LibBrowse
			// 
			this.LibBrowse.Location = new System.Drawing.Point(192, 68);
			this.LibBrowse.Name = "LibBrowse";
			this.LibBrowse.Size = new System.Drawing.Size(24, 20);
			this.LibBrowse.TabIndex = 18;
			this.LibBrowse.Text = "...";
			this.LibBrowse.Click += new System.EventHandler(this.LibBrowseClick);
			// 
			// pNeko
			// 
			this.pNeko.Location = new System.Drawing.Point(134, 48);
			this.pNeko.Name = "pNeko";
			this.pNeko.Size = new System.Drawing.Size(48, 16);
			this.pNeko.TabIndex = 9;
			this.pNeko.Text = "Neko";
			this.pNeko.CheckedChanged += new System.EventHandler(this.outputChanged);
			// 
			// pOutput
			// 
			this.pOutput.Location = new System.Drawing.Point(64, 16);
			this.pOutput.Name = "pOutput";
			this.pOutput.Size = new System.Drawing.Size(152, 20);
			this.pOutput.TabIndex = 1;
			this.pOutput.Text = "";
			this.pOutput.Leave += new System.EventHandler(this.outputChanged);
			// 
			// Props
			// 
			this.Props.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
						| System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.Props.Controls.Add(this.pPHP);
			this.Props.Controls.Add(this.LExtra);
			this.Props.Controls.Add(this.pExtra);
			this.Props.Controls.Add(this.Html);
			this.Props.Controls.Add(this.pCommand);
			this.Props.Controls.Add(this.LCommand);
			this.Props.Controls.Add(this.LibBrowse);
			this.Props.Controls.Add(this.pLib);
			this.Props.Controls.Add(this.LLib);
			this.Props.Controls.Add(this.pDebug);
			this.Props.Controls.Add(this.pMain);
			this.Props.Controls.Add(this.ClassBrowse);
			this.Props.Controls.Add(this.pClass);
			this.Props.Controls.Add(this.LClass);
			this.Props.Controls.Add(this.pFlash9);
			this.Props.Controls.Add(this.pNeko);
			this.Props.Controls.Add(this.pJS);
			this.Props.Controls.Add(this.pFlash);
			this.Props.Controls.Add(this.pOutput);
			this.Props.Controls.Add(this.LPOut);
			this.Props.Location = new System.Drawing.Point(0, 168);
			this.Props.Name = "Props";
			this.Props.Size = new System.Drawing.Size(224, 376);
			this.Props.TabIndex = 2;
			this.Props.TabStop = false;
			this.Props.Text = "Properties";
			this.Props.Visible = false;
			// 
			// pDebug
			// 
			this.pDebug.Location = new System.Drawing.Point(16, 152);
			this.pDebug.Name = "pDebug";
			this.pDebug.Size = new System.Drawing.Size(120, 16);
			this.pDebug.TabIndex = 15;
			this.pDebug.Text = "Debug Mode";
			this.pDebug.CheckedChanged += new System.EventHandler(this.propChanged);
			// 
			// omSelect
			// 
			this.omSelect.Index = 2;
			this.omSelect.Text = "Select Current";
			this.omSelect.Click += new System.EventHandler(this.menuCommand);
			// 
			// LLib
			// 
			this.LLib.Location = new System.Drawing.Point(8, 72);
			this.LLib.Name = "LLib";
			this.LLib.Size = new System.Drawing.Size(56, 16);
			this.LLib.TabIndex = 16;
			this.LLib.Text = "Library :";
			// 
			// omDelete
			// 
			this.omDelete.Index = 4;
			this.omDelete.Text = "Delete";
			this.omDelete.Click += new System.EventHandler(this.menuCommand);
			// 
			// omenu
			// 
			this.omenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.omAdd,
						this.break1,
						this.omSelect,
						this.omRename,
						this.omDelete,
						this.omActive});
			// 
			// pExtra
			// 
			this.pExtra.AcceptsReturn = true;
			this.pExtra.AcceptsTab = true;
			this.pExtra.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.pExtra.Location = new System.Drawing.Point(8, 239);
			this.pExtra.Multiline = true;
			this.pExtra.Name = "pExtra";
			this.pExtra.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.pExtra.Size = new System.Drawing.Size(208, 128);
			this.pExtra.TabIndex = 5;
			this.pExtra.Text = "";
			this.pExtra.WordWrap = false;
			this.pExtra.TextChanged += new System.EventHandler(this.propChanged);
			// 
			// omAdd
			// 
			this.omAdd.Index = 0;
			this.omAdd.Text = "Add Target";
			this.omAdd.Click += new System.EventHandler(this.menuCommand);
			// 
			// Html
			// 
			this.Html.Location = new System.Drawing.Point(144, 200);
			this.Html.Name = "Html";
			this.Html.Size = new System.Drawing.Size(72, 18);
			this.Html.TabIndex = 21;
			this.Html.Text = "Create html";
			this.Html.Click += new System.EventHandler(this.HtmlClick);
			// 
			// LCommand
			// 
			this.LCommand.Location = new System.Drawing.Point(8, 180);
			this.LCommand.Name = "LCommand";
			this.LCommand.Size = new System.Drawing.Size(64, 24);
			this.LCommand.TabIndex = 19;
			this.LCommand.Text = "Test Cmd:";
			// 
			// break1
			// 
			this.break1.Index = 1;
			this.break1.Text = "-";
			// 
			// pClass
			// 
			this.pClass.Location = new System.Drawing.Point(64, 96);
			this.pClass.Name = "pClass";
			this.pClass.Size = new System.Drawing.Size(120, 20);
			this.pClass.TabIndex = 12;
			this.pClass.Text = "";
			this.pClass.TextChanged += new System.EventHandler(this.propChanged);
			// 
			// pJS
			// 
			this.pJS.Location = new System.Drawing.Point(102, 48);
			this.pJS.Name = "pJS";
			this.pJS.Size = new System.Drawing.Size(34, 16);
			this.pJS.TabIndex = 8;
			this.pJS.Text = "JS";
			this.pJS.CheckedChanged += new System.EventHandler(this.outputChanged);
			// 
			// PluginUI
			// 
			this.Controls.Add(this.Props);
			this.Controls.Add(this.Outputs);
			this.Controls.Add(this.PName);
			this.Name = "PluginUI";
			this.Size = new System.Drawing.Size(224, 544);
			this.Props.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion

		#region Output Context Menu

		ProjectOutput curoutput {
			get {
				TreeNode n = Outputs.SelectedNode;
				if( n == null )
					return null;
				return n.Tag as ProjectOutput;
			}
		}

		void menuCommand(object sender, System.EventArgs e)	{
			if( sender == omAdd )
				plugin.addOutput();
			else if( sender == omSelect )
				plugin.selectOutput(curoutput);
			else if( sender == omRename )
				Outputs.SelectedNode.BeginEdit();				
			else if( sender == omDelete) {
				if( Dialog.Confirm("Delete Output "+curoutput.name+" ?") )
					plugin.deleteOutput(curoutput);
			} else if( sender == omActive ) 
				plugin.toggleOutput(curoutput);
		}
		
		void OutputsMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if( e.Button == MouseButtons.Right ) {
				Point pt = new Point(e.X,e.Y);
				Outputs.SelectedNode = Outputs.GetNodeAt(pt);
				bool ext = (curoutput != null);
				omSelect.Enabled = ext;
				omRename.Enabled = ext;
				omDelete.Enabled = ext;
				omActive.Enabled = ext;
				omActive.Checked = ext && curoutput.active;
				omenu.Show(Outputs,pt);
			}
		}		

		void OutputsBeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			if( (e.Node.Tag as ProjectOutput) == null )			
				e.CancelEdit = true;
		}

		void OutputsAfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			ProjectOutput o = e.Node.Tag as ProjectOutput;
			o.name = e.Label;			
		}

		void OutputsAfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			plugin.refreshProperties(curoutput);
		}

		#endregion

		#region Properties
		
		public ProjectOutput curprop {
			get {
				return Props.Tag as ProjectOutput;
			}
		}

		void outputChanged(object sender, System.EventArgs e)
		{
			ProjectOutput o = curprop;
			if( o == null )
				return;
			if( pFlash.Checked )
				o.mode = ProjectOutput.FLASH;
			else if( pFlash9.Checked )
				o.mode = ProjectOutput.FLASH9;
			else if( pJS.Checked )
				o.mode = ProjectOutput.JS;
			else if( pPHP.Checked )
				o.mode = ProjectOutput.PHP;
			else
				o.mode = ProjectOutput.NEKO;
			String dir = Path.GetDirectoryName(pOutput.Text);
			if( dir != "" )
				dir = dir.Replace('\\','/') + "/";
			
			string [] at = pOutput.Text.Split('@');
			pOutput.Text = dir + Path.GetFileNameWithoutExtension(pOutput.Text) + o.Extension() + ((at.Length > 1)?"@"+at[1]:"");
			o.output = pOutput.Text;

			pLib.Enabled = (o.mode == ProjectOutput.FLASH || o.mode == ProjectOutput.FLASH9);
			Html.Enabled = pLib.Enabled || o.mode == ProjectOutput.JS;
			LibBrowse.Enabled = pLib.Enabled;
		}

		void ClassBrowseClick(object sender, System.EventArgs e)
		{
			String file = Dialog.Open(Dialog.HX);
			if( file == null )
				return;
			String dir = Path.GetDirectoryName(file);
			String pdir = Path.GetDirectoryName(PluginMain.project.file);
			if( !dir.StartsWith(pdir) ) {
				Dialog.Error("The class should be relative to the project file");
				return;
			}
			String cl = Path.GetFileNameWithoutExtension(file.Substring(pdir.Length+1).Replace('\\','.'));
			pClass.Text = cl;
		}

		void LibBrowseClick(object sender, System.EventArgs e)
		{
			String file = Dialog.Open(Dialog.SWF);
			if( file == null ) {
				pLib.Text = "";
				return;
			}
			String dir = Path.GetDirectoryName(file);
			String pdir = Path.GetDirectoryName(PluginMain.project.file);
			if( dir.StartsWith(pdir) )
				pLib.Text = file.Substring(pdir.Length+1);
			else
				pLib.Text = file;
		}

		void propChanged(object sender, System.EventArgs e) {
			if( sender == pClass )
				curprop.mainClass = pClass.Text;
			else if( sender == pLib )
				curprop.library = pLib.Text;
			else if( sender == pMain )
				curprop.mainCall = pMain.Checked;
			else if( sender == pDebug )
				curprop.debug = pDebug.Checked;
			else if( sender == pExtra )
				curprop.extra = pExtra.Text;
			else if( sender == pCommand )
				curprop.command = pCommand.Text;
		}

		void HtmlClick(object sender, System.EventArgs e)
		{
			String s = plugin.generateHtml(curprop);
			if( s != null ) {
				pCommand.Text = s;
				curprop.command = s;
			}
		}
		
		#endregion
		
	}
}
